<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>决策树特征选择可视化指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f9fafb;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .code-block {
            font-family: 'Courier New', Courier, monospace;
            background-color: #2d2d2d;
            color: #f8f8f2;
            border-radius: 8px;
            position: relative;
        }
        .code-copy-btn {
            position: absolute;
            right: 10px;
            top: 10px;
            background: rgba(255, 255, 255, 0.1);
            border: none;
            color: white;
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.2s;
        }
        .code-copy-btn:hover {
            background: rgba(255, 255, 255, 0.2);
        }
        .mermaid-tooltip {
            position: absolute;
            background: white;
            padding: 10px;
            border-radius: 6px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            z-index: 100;
            max-width: 300px;
            display: none;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4">
        <div class="max-w-6xl mx-auto">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold leading-tight mb-4">决策树特征选择算法</h1>
                    <p class="text-xl text-indigo-100 mb-8">探索信息增益与基尼指数的奥秘</p>
                    <div class="flex space-x-4">
                        <div class="bg-white bg-opacity-20 px-6 py-3 rounded-lg flex items-center">
                            <i class="fas fa-project-diagram mr-2"></i>
                            <span>机器学习</span>
                        </div>
                        <div class="bg-white bg-opacity-20 px-6 py-3 rounded-lg flex items-center">
                            <i class="fas fa-brain mr-2"></i>
                            <span>决策树</span>
                        </div>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="bg-white bg-opacity-20 p-6 rounded-xl backdrop-blur-sm">
                        <div class="mermaid">
                            graph TD
                            A[决策树特征选择] --> B[信息增益]
                            A --> C[基尼指数]
                            B --> D[熵的计算 H = -Σ(p * log(p))]
                            C --> E[Gini = 1 - Σ(p²)]
                            D --> F[分裂前熵 - 分裂后加权熵]
                            E --> G[选择最小Gini的特征]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Content Section -->
    <section class="py-16 px-4">
        <div class="max-w-6xl mx-auto">
            <!-- Introduction -->
            <div class="mb-16">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2">算法简介</h2>
                <div class="grid md:grid-cols-2 gap-8">
                    <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 p-3 rounded-lg mr-4">
                                <i class="fas fa-info-circle text-indigo-600 text-xl"></i>
                            </div>
                            <h3 class="text-xl font-semibold">信息增益</h3>
                        </div>
                        <p class="text-gray-600">信息增益衡量的是特征对分类不确定性的减少程度。通过计算分裂前后熵的差值，我们可以评估该特征对分类任务的贡献。</p>
                        <div class="mt-4">
                            <div class="bg-blue-50 p-3 rounded-lg">
                                <p class="text-blue-800 font-medium">信息增益 = 分裂前的熵 - 分裂后的加权熵</p>
                            </div>
                        </div>
                    </div>
                    <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                        <div class="flex items-center mb-4">
                            <div class="bg-purple-100 p-3 rounded-lg mr-4">
                                <i class="fas fa-chart-pie text-purple-600 text-xl"></i>
                            </div>
                            <h3 class="text-xl font-semibold">基尼指数</h3>
                        </div>
                        <p class="text-gray-600">基尼指数衡量的是一个随机样本被错误分类的概率。基尼指数越小，表示数据集的纯度越高。</p>
                        <div class="mt-4">
                            <div class="bg-purple-50 p-3 rounded-lg">
                                <p class="text-purple-800 font-medium">基尼指数 = 1 - Σ(每个类别的概率²)</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Algorithm Details -->
            <div class="mb-16">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2">算法详解</h2>
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h3 class="text-2xl font-semibold mb-4 flex items-center">
                            <span class="bg-indigo-600 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3">1</span>
                            信息增益计算步骤
                        </h3>
                        <div class="space-y-4">
                            <div class="flex items-start">
                                <div class="bg-indigo-100 text-indigo-800 rounded-full w-6 h-6 flex-shrink-0 flex items-center justify-center mt-1 mr-3">①</div>
                                <p>计算分裂前数据集的熵</p>
                            </div>
                            <div class="flex items-start">
                                <div class="bg-indigo-100 text-indigo-800 rounded-full w-6 h-6 flex-shrink-0 flex items-center justify-center mt-1 mr-3">②</div>
                                <p>根据特征值将数据集划分为若干子集</p>
                            </div>
                            <div class="flex items-start">
                                <div class="bg-indigo-100 text-indigo-800 rounded-full w-6 h-6 flex-shrink-0 flex items-center justify-center mt-1 mr-3">③</div>
                                <p>计算每个子集的熵，并按子集规模加权求和</p>
                            </div>
                            <div class="flex items-start">
                                <div class="bg-indigo-100 text-indigo-800 rounded-full w-6 h-6 flex-shrink-0 flex items-center justify-center mt-1 mr-3">④</div>
                                <p>用分裂前熵减去加权后的分裂后熵得到信息增益</p>
                            </div>
                        </div>
                    </div>
                    <div>
                        <h3 class="text-2xl font-semibold mb-4 flex items-center">
                            <span class="bg-purple-600 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3">2</span>
                            基尼指数计算步骤
                        </h3>
                        <div class="space-y-4">
                            <div class="flex items-start">
                                <div class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex-shrink-0 flex items-center justify-center mt-1 mr-3">①</div>
                                <p>计算每个特征值对应的子集基尼指数</p>
                            </div>
                            <div class="flex items-start">
                                <div class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex-shrink-0 flex items-center justify-center mt-1 mr-3">②</div>
                                <p>计算每个子集在总体中的权重</p>
                            </div>
                            <div class="flex items-start">
                                <div class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex-shrink-0 flex items-center justify-center mt-1 mr-3">③</div>
                                <p>计算加权基尼指数</p>
                            </div>
                            <div class="flex items-start">
                                <div class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex-shrink-0 flex items-center justify-center mt-1 mr-3">④</div>
                                <p>选择基尼指数最小的特征作为分裂特征</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Code Example -->
            <div class="mb-16">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2">代码实现</h2>
                <div class="bg-white rounded-xl shadow-md overflow-hidden">
                    <div class="bg-gray-800 px-4 py-3 flex items-center">
                        <div class="flex space-x-2 mr-4">
                            <span class="w-3 h-3 rounded-full bg-red-500"></span>
                            <span class="w-3 h-3 rounded-full bg-yellow-500"></span>
                            <span class="w-3 h-3 rounded-full bg-green-500"></span>
                        </div>
                        <p class="text-gray-300">DecisionTreeFeatureSelection.java</p>
                    </div>
                    <div class="p-4">
                        <div class="code-block rounded-lg p-4 mb-6 relative">
                            <button class="code-copy-btn" onclick="copyCode(this)">
                                <i class="far fa-copy"></i> 复制
                            </button>
                            <pre><code>/**
 * 计算特征的信息增益
 * 信息增益 = 分裂前的熵 - 分裂后的加权熵
 * 
 * @param data 数据集，每行是一个样本，列为特征值
 * @param feature 要计算信息增益的特征索引
 * @param target 目标变量（分类标签）的索引
 * @return 该特征的信息增益值
 */
public static double informationGain(List&lt;List&lt;String&gt;&gt; data, int feature, int target) {
    // 计算分裂前的熵（目标变量的熵）
    List&lt;String&gt; targetColumn = getColumn(data, target);
    double totalEntropy = entropy(targetColumn);
    
    // 获取特征的所有可能取值
    Set&lt;String&gt; featureValues = new HashSet&lt;&gt;(getColumn(data, feature));
    
    // 计算分裂后的加权熵
    double weightedEntropy = 0.0;
    for (String value : featureValues) {
        // 获取特征值等于value的子集
        List&lt;List&lt;String&gt;&gt; subset = getSubset(data, feature, value);
        
        // 计算子集在总体中的权重
        double weight = (double) subset.size() / data.size();
        
        // 计算子集的目标变量熵
        List&lt;String&gt; subsetTargets = getColumn(subset, target);
        double subsetEntropy = entropy(subsetTargets);
        
        // 累加加权熵
        weightedEntropy += weight * subsetEntropy;
    }
    
    // 返回信息增益
    return totalEntropy - weightedEntropy;
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Visualization -->
            <div class="mb-16">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2">可视化分析</h2>
                <div class="grid md:grid-cols-2 gap-8">
                    <div class="bg-white rounded-xl shadow-md p-6">
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-chart-line text-blue-500 mr-2"></i>
                            信息增益对比
                        </h3>
                        <div class="mermaid">
                            pie title 信息增益示例
                                "天气特征" : 0.247
                                "温度特征" : 0.029
                                "湿度特征" : 0.152
                        </div>
                        <p class="text-gray-600 mt-4">从图中可以看出，天气特征的信息增益最大，是最佳分裂特征。</p>
                    </div>
                    <div class="bg-white rounded-xl shadow-md p-6">
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-project-diagram text-purple-500 mr-2"></i>
                            决策树构建过程
                        </h3>
                        <div class="mermaid">
                            graph TD
                            A[是否打网球?] -->|天气=晴朗| B[否]
                            A -->|天气=多云| C[是]
                            A -->|天气=雨| D[湿度]
                            D -->|湿度=高| E[是]
                            D -->|湿度=正常| F[否]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Performance Section -->
    <section class="py-16 px-4 bg-gray-50">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-2">算法性能分析</h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="bg-green-100 p-3 rounded-lg mr-4">
                            <i class="fas fa-stopwatch text-green-600 text-xl"></i>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold">时间复杂度</h3>
                            <p class="text-gray-500">O(n × m)</p>
                        </div>
                    </div>
                    <p class="text-gray-600">n为样本数，m为特征数。需要在每个特征上计算信息增益或基尼指数。</p>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="bg-blue-100 p-3 rounded-lg mr-4">
                            <i class="fas fa-memory text-blue-600 text-xl"></i>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold">空间复杂度</h3>
                            <p class="text-gray-500">O(m)</p>
                        </div>
                    </div>
                    <p class="text-gray-600">需要存储每个特征的信息增益或基尼指数结果。</p>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 p-3 rounded-lg mr-4">
                            <i class="fas fa-balance-scale text-purple-600 text-xl"></i>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold">适用场景</h3>
                            <p class="text-gray-500">分类任务</p>
                        </div>
                    </div>
                    <p class="text-gray-600">特别适合处理离散特征和类别不平衡的数据集。</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Application Example -->
    <section class="py-16 px-4">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2">应用示例</h2>
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4">天气与网球决策数据集</h3>
                    <div class="overflow-x-auto">
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">天气</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">温度</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">湿度</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">打网球</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap">晴朗</td>
                                    <td class="px-6 py-4 whitespace-nowrap">热</td>
                                    <td class="px-6 py-4 whitespace-nowrap">高</td>
                                    <td class="px-6 py-4 whitespace-nowrap">否</td>
                                </tr>
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap">晴朗</td>
                                    <td class="px-6 py-4 whitespace-nowrap">热</td>
                                    <td class="px-6 py-4 whitespace-nowrap">高</td>
                                    <td class="px-6 py-4 whitespace-nowrap">否</td>
                                </tr>
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap">多云</td>
                                    <td class="px-6 py-4 whitespace-nowrap">热</td>
                                    <td class="px-6 py-4 whitespace-nowrap">高</td>
                                    <td class="px-6 py-4 whitespace-nowrap">是</td>
                                </tr>
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap">雨</td>
                                    <td class="px-6 py-4 whitespace-nowrap">适中</td>
                                    <td class="px-6 py-4 whitespace-nowrap">高</td>
                                    <td class="px-6 py-4 whitespace-nowrap">是</td>
                                </tr>
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap">雨</td>
                                    <td class="px-6 py-4 whitespace-nowrap">凉爽</td>
                                    <td class="px-6 py-4 whitespace-nowrap">正常</td>
                                    <td class="px-6 py-4 whitespace-nowrap">是</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="mt-6 bg-blue-50 p-4 rounded-lg">
                        <h4 class="font-semibold text-blue-800 mb-2">分析结果</h4>
                        <p>对于这个数据集，计算得到各特征的信息增益分别为：天气(0.247)、温度(0.029)、湿度(0.152)。因此，选择"天气"作为第一分裂特征。</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });

        function copyCode(button) {
            const codeBlock = button.parentElement.querySelector('code');
            const range = document.createRange();
            range.selectNode(codeBlock);
            window.getSelection().removeAllRanges();
            window.getSelection().addRange(range);
            document.execCommand('copy');
            window.getSelection().removeAllRanges();
            
            // 显示复制成功提示
            const originalText = button.innerHTML;
            button.innerHTML = '<i class="fas fa-check"></i> 已复制';
            setTimeout(() => {
                button.innerHTML = originalText;
            }, 2000);
        }

        // 添加mermaid图表交互
        document.addEventListener('DOMContentLoaded', function() {
            const mermaidElements = document.querySelectorAll('.mermaid');
            mermaidElements.forEach(el => {
                el.addEventListener('mouseover', function(e) {
                    if(e.target.tagName === 'text') {
                        const tooltip = document.createElement('div');
                        tooltip.className = 'mermaid-tooltip';
                        tooltip.textContent = e.target.textContent;
                        document.body.appendChild(tooltip);
                        
                        const position = e.target.getBoundingClientRect();
                        tooltip.style.left = `${position.left + position.width/2}px`;
                        tooltip.style.top = `${position.top - 40}px`;
                        tooltip.style.display = 'block';
                        
                        e.target.addEventListener('mouseout', function() {
                            tooltip.remove();
                        }, {once: true});
                    }
                });
            });
        });
    </script>
</body>
</html>